package com.motu.vertx.module.utility.threadpool;

import com.motu.vertx.module.utility.kafka.KafkaLogManager;
import com.motu.vertx.module.utility.toolset.ErrorWord;
import com.motu.vertx.module.utility.toolset.TimeUtils;
import io.vertx.core.json.JsonArray;

/**
 * 数据库日志管理父类
 */
public class DBLogManager {

	public static final int DBTYPE_SERVER = 0;//连接server库
	public static final int DBTYPE_COMMON = 1;//连接common库
	public static final int DBTYPE_WORLD = 2;//连接world库
	public static final int DBTYPE_GAME = 3;//连接game库
	public static final int DBTYPE_WEB = 4;//连接web库
	public static final int DBTYPE_LOG = 5;//连接log库

	/**
	 * 记录错误码日志
	 */
	public static void addErrorCode(long userId, String cmd, String params, ErrorWord errorWord, int cmdCode, byte[] request) {
		String sql = "INSERT INTO error_code(user_id,cmd,params,err_code,cmd_code,request,add_time) VALUES(?,?,?,?,?,?,?);";
		JsonArray sqlParams = new JsonArray();
		sqlParams.add(userId);
		sqlParams.add(cmd);
		sqlParams.add(params != null ? params : "");
		sqlParams.add(errorWord.name());
		sqlParams.add(cmdCode);
		sqlParams.add(request);
		sqlParams.add(TimeUtils.getCurTime());
		KafkaLogManager.addGameLog(sql, sqlParams, DBTYPE_SERVER);
	}

	/**
	 * 记录代码异常日志
	 */
	public static void addServerCrashLog(long userId, String errMsg) {
		String sql = "INSERT INTO server_crash_log(user_id,content,add_time) values(?,?,?);";
		JsonArray sqlParams = new JsonArray().add(userId).add(errMsg).add(TimeUtils.getCurTime());
		KafkaLogManager.addGameLog(sql, sqlParams, DBTYPE_SERVER);
	}

}
